//
//  VerifyPhoneView.m
//  TaoZhiHuiProj
//
//  Created by 焱 孙 on 15/9/6.
//  Copyright (c) 2015年 visionet. All rights reserved.
//

#import "TicketCommitView.h"
#import "UIViewExt.h"
#import "UserVo.h"
#import "InsetsTextField.h"
#import "CustomDatePicker.h"
#import "ChooseEngineerViewController.h"

@interface TicketCommitView ()<UITextFieldDelegate,CustomDatePickerDelegate,ChooseEngineerDelegate>
{
    UIView *viewContainer;

    UILabel *lblTip;
    InsetsTextField *txtItem1;      //预计上门时间 或者 转移工程师
    CustomDatePicker *pickerDate;
    
    UILabel *lblRemark;
    UITextView *txtRemark;
    
    UIButton *btnCancel;
    UIButton *btnConfirm;
    
    NSInteger nViewType; //0:接受、1:转移、2:远程完成
    UserVo *userVoEngineer; //转移的工程师
    ChooseEngineerViewController *chooseEngineerViewController;
}

@end

@implementation TicketCommitVo

@end

@implementation TicketCommitView

- (instancetype)initWithFrame:(CGRect)frame type:(NSInteger)nType delegate:(id<TicketCommitViewDelegate>)delegate
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.delegate = delegate;
        nViewType = nType;
        
        self.backgroundColor = COLOR(0, 0, 0, 0.4);
        
        viewContainer = [[UIView alloc]initWithFrame:CGRectMake((kScreenWidth-280)/2, 100, 280, 200)];
        viewContainer.backgroundColor = [UIColor whiteColor];
        viewContainer.layer.cornerRadius = 4;
        viewContainer.layer.masksToBounds = YES;
        viewContainer.layer.borderWidth = 0;
        [self addSubview:viewContainer];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBackground)];
        [self addGestureRecognizer:tapGesture];
        
        lblTip = [[UILabel alloc]initWithFrame:CGRectMake(20, 15, 240, 20)];
        lblTip.font = [UIFont systemFontOfSize:16];
        lblTip.textColor = COLOR(51, 51, 51, 1.0);
        [viewContainer addSubview:lblTip];
        
        txtItem1 = [[InsetsTextField alloc]initWithFrame:CGRectMake(20, lblTip.bottom+5, 240, 36)];
        txtItem1.delegate = self;
        txtItem1.layer.cornerRadius = 4;
        txtItem1.layer.masksToBounds = YES;
        txtItem1.layer.borderWidth = 0.5;
        txtItem1.layer.borderColor = COLOR(204, 204, 204, 1.0).CGColor;
        txtItem1.font = [UIFont systemFontOfSize:15];
        txtItem1.keyboardType = UIKeyboardTypeNumberPad;
        [viewContainer addSubview:txtItem1];
        
        pickerDate = [[CustomDatePicker alloc] initWithFrame:CGRectMake(0, kScreenHeight-260, kScreenWidth, 260) andDelegate:self];
        pickerDate.pickerView.minimumDate = [NSDate date];
        pickerDate.hidden = YES;
        [self addSubview:pickerDate];
        
        lblRemark = [[UILabel alloc]initWithFrame:CGRectMake(20, txtItem1.bottom+10, 240, 20)];
        lblRemark.text = @"备注";
        lblRemark.font = [UIFont systemFontOfSize:16];
        lblRemark.textColor = COLOR(51, 51, 51, 1.0);
        [viewContainer addSubview:lblRemark];
        
        txtRemark = [[UITextView alloc]initWithFrame:CGRectMake(20, lblRemark.bottom+5, 240, 60)];
        txtRemark.layer.cornerRadius = 4;
        txtRemark.layer.masksToBounds = YES;
        txtRemark.layer.borderWidth = 0.5;
        txtRemark.layer.borderColor = COLOR(204, 204, 204, 1.0).CGColor;
        txtRemark.font = [UIFont systemFontOfSize:15];
        [viewContainer addSubview:txtRemark];
        
        btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
        btnCancel.layer.cornerRadius = 4;
        btnCancel.layer.borderWidth = 0;
        btnCancel.layer.masksToBounds = YES;
        btnCancel.frame = CGRectMake(30, txtItem1.bottom+17, 90, 36);
        [btnCancel setBackgroundImage:[Common getImageWithColor:COLOR(153, 153, 153, 1.0)] forState:UIControlStateNormal];
        [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
        [btnCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnCancel addTarget:self action:@selector(verifyAction:) forControlEvents:UIControlEventTouchUpInside];
        [viewContainer addSubview:btnCancel];
        
        btnConfirm = [UIButton buttonWithType:UIButtonTypeCustom];
        btnConfirm.layer.cornerRadius = 4;
        btnConfirm.layer.borderWidth = 0;
        btnConfirm.layer.masksToBounds = YES;
        btnConfirm.frame = CGRectMake(btnCancel.right+9.5+30, btnCancel.top, 90, 36);
        [btnConfirm setBackgroundImage:[Common getImageWithColor:THEME_COLOR] forState:UIControlStateNormal];
        [btnConfirm setTitle:@"确认" forState:UIControlStateNormal];
        [btnConfirm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnConfirm addTarget:self action:@selector(verifyAction:) forControlEvents:UIControlEventTouchUpInside];
        [viewContainer addSubview:btnConfirm];
        
        if (nType == 0)
        {
            lblTip.text = @"请选择预计上门时间";
            txtItem1.placeholder = @"预计上门时间";
            
            lblRemark.hidden = YES;
            txtRemark.hidden = YES;
            
            pickerDate.hidden = NO;
            
            //frame
            btnCancel.frame = CGRectMake(30, txtItem1.bottom+25, 90, 36);
            btnConfirm.frame = CGRectMake(btnCancel.right+9.5+30, btnCancel.top, 90, 36);
            viewContainer.height = 162;
        }
        else if (nType == 1)
        {
            lblTip.text = @"请选择工程师";
            txtItem1.placeholder = @"选择工程师";
            
            //frame
            btnCancel.frame = CGRectMake(30, txtRemark.bottom+25, 90, 36);
            btnConfirm.frame = CGRectMake(btnCancel.right+9.5+30, btnCancel.top, 90, 36);
            viewContainer.height = 257;
        }
        else
        {
            lblTip.hidden = YES;
            txtItem1.hidden = YES;
            
            //frame
            lblRemark.frame = CGRectMake(20, 15, 240, 20);
            lblRemark.text = @"远程完成备注";
            txtRemark.frame = CGRectMake(20, lblRemark.bottom+5, 240, 60);
            
            btnCancel.frame = CGRectMake(30, txtRemark.bottom+25, 90, 36);
            btnConfirm.frame = CGRectMake(btnCancel.right+9.5+30, btnCancel.top, 90, 36);
            viewContainer.height = 186;
        }
    }
    
    return self;
}

- (void)verifyAction:(UIButton *)sender
{
    if (sender == btnCancel)
    {
        [self.delegate completedAction:nViewType data:nil];
    }
    else
    {
        if (nViewType == 0)
        {
            //0:接受 [预计上门时间可空]
            TicketCommitVo *commitVo = [[TicketCommitVo alloc]init];
            commitVo.strServiceTime = txtItem1.text;
            [self.delegate completedAction:nViewType data:commitVo];
        }
        else if (nViewType == 1)
        {
            //1:转移
            if(userVoEngineer == nil)
            {
                [Common tipAlert:@"请选择接受的工程师"];
            }
            else
            {
                TicketCommitVo *commitVo = [[TicketCommitVo alloc]init];
                commitVo.userVo = userVoEngineer;
                commitVo.strRemark = txtRemark.text;
                [self.delegate completedAction:nViewType data:commitVo];
            }
        }
        else
        {
            //2:远程完成 [必须填写备注]
            if(txtRemark.text.length == 0)
            {
                [Common tipAlert:@"远程完成操作需要填写备注"];
            }
            else
            {
                TicketCommitVo *commitVo = [[TicketCommitVo alloc]init];
                commitVo.strRemark = txtRemark.text;
                [self.delegate completedAction:nViewType data:commitVo];
            }
        }
    }
}

- (void)tapBackground
{
    [txtItem1 resignFirstResponder];
    [txtRemark resignFirstResponder];
}

- (void)clearText
{
    txtItem1.text = nil;
    txtRemark.text = nil;
}

- (void)refreshView
{
    if (nViewType == 0)
    {
        //0:接受
        [self setPickerHidden:pickerDate andHide:NO];
    }
    else if (nViewType == 1)
    {
        //1:转移
        
    }
    else
    {
        //2:远程完成 [必须填写备注]
        [txtRemark becomeFirstResponder];
    }
}

//显示隐藏时间控件
- (void)setPickerHidden:(UIView*)pickerViewCtrl andHide:(BOOL)hidden
{
    CGFloat fOffsetY;
    if (hidden)
    {
        fOffsetY = kScreenHeight;
    }
    else
    {
        fOffsetY = kScreenHeight-260;
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        pickerDate.frame = CGRectMake(0, fOffsetY, kScreenWidth, 260);
    } completion:nil];
}

//选择日期
-(void)doSelectDate:(UITextField*)txtView
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    if (txtView == txtItem1)
    {
        //出生日期
        [self setPickerHidden:pickerDate andHide:NO];
        NSString *strDate = txtView.text;
        if (strDate.length>0)
        {
            NSDate *date = [Common getDateFromDateStr:strDate];
            if (date != nil)
            {
                [pickerDate.pickerView setDate:date animated:YES];
            }
        }
    }
}

#pragma mark - ChooseEngineerDelegate
- (void)completedChooseEngineer:(UserVo *)userVo
{
    userVoEngineer = userVo;
    txtItem1.text = userVoEngineer.strUserName;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == txtItem1)
    {
        if (nViewType == 0)
        {
            //接受
            [self doSelectDate:textField];
        }
        else
        {
            //转移
            if(chooseEngineerViewController == nil)
            {
                chooseEngineerViewController  = [[UIStoryboard storyboardWithName:@"TicketModule" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"ChooseEngineerViewController"];
                chooseEngineerViewController.delegate = self;
            }
            UIViewController *parentController = (UIViewController *)self.delegate;
            [parentController.navigationController pushViewController:chooseEngineerViewController animated:YES];
        }
        return NO;
    }
    else
    {
        return YES;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - CustomDatePickerDelegate
- (void)doneDatePickerButtonClick:(CustomDatePicker*)pickerViewCtrl
{
    [self setPickerHidden:pickerViewCtrl andHide:YES];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    if (pickerViewCtrl == pickerDate)
    {
        NSDate *date = pickerViewCtrl.pickerView.date;
        txtItem1.text = [dateFormatter stringFromDate:date];
    }
}

- (void)cancelDatePickerButtonClick:(CustomDatePicker*)pickerViewCtrl
{
    [self setPickerHidden:pickerViewCtrl andHide:YES];
}

@end
